package com.jadcon.bedbreakfast.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.bedbreakfast.domain.Reservation;
import com.jadcon.bedbreakfast.domain.ReservationService;

@Controller
public class ViewReservationController {

	@Autowired
	private ReservationService reservationService;

	@RequestMapping("/reservationSearch.html")
	public ModelAndView reservationDetail() {
		System.out.println("ReservationController.reservationDetail() begin...");
		// get data from model
		List<Reservation> reservationList = reservationService
				.getReservationList();

		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationSearch");
		mav.addObject("reservationList", reservationList);
		System.out.println("added object to reservation list...");
		return mav;
	}
	
	@RequestMapping("/findReservationByNumber.html")
	public ModelAndView reservationSearch(@RequestParam int reservationNumber) {
		System.out.println("ReservationController.reservationSearch() begin...");
		
		// get data from model
		Reservation reservation = reservationService.findByNumber(reservationNumber);
		List<Reservation> reservationList = new ArrayList<Reservation>();
		
		if (reservation == null){
			return new ModelAndView("reservationSearchNoResults");
		}
		
		else {
		reservationList.add(reservation);
		
		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationSearchResults");
		//mav.addObject("reservation", reservation);
		mav.addObject("reservationList", reservationList);
		return mav;
		}
	}

	@RequestMapping("/findReservationByRoom.html")
	public ModelAndView findReservationByRoom(@RequestParam int roomNumber) {
		System.out.println("ReservationController.reservationRoomSearch() begin...");

		// get data from model
		List<Reservation> reservationList = reservationService.findReservationByRoom(roomNumber);

		if (reservationList.isEmpty()){
			return new ModelAndView("reservationSearchNoResults");
		}
		
		else {
		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationSearchResults"); 
		mav.addObject("reservationList", reservationList);
		return mav;
		}
	}

	@RequestMapping("/findReservationByStartDate.html")
	public ModelAndView findReservationByStartDate(@RequestParam Date checkIn) {
		System.out.println("ReservationController.findReservationByStartDate() begin...");

		// get data from model
		List<Reservation> reservationList = reservationService
				.findReservationByStartDate(checkIn);
		
		if (reservationList.isEmpty()){
			return new ModelAndView("reservationSearchNoResults");
		}
		
		else {

		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationSearchResults");
		mav.addObject("reservationList", reservationList);
		return mav;
		}
	}
	
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

	public void setReservationService(ReservationService reservationService) {
		this.reservationService = reservationService;
	}

}
